#pragma once

#include "poller.h"
#include "timestamp.h"

#include <sys/epoll.h>
#include <vector>

/*
epoll_create
epoll_ctl add/mod/del
epoll_wait
*/
class EPollPoller : public Poller {
  public:
    EPollPoller(EventLoop *loop);
    ~EPollPoller() override;

    // 重写基类的Poller的抽象方法
    Timestamp poll(int timeout_ms, ChannelList *active_channels) override;
    void update_channel(Channel *channel) override;
    void remove_channel(Channel *channel) override;

  private:
    static const int kInitEventListSize = 16;

    // 填写活跃的连接
    void fill_active_channels(int num_events, ChannelList *active_channels) const;
    // 更新channel通道
    void update(int operation, Channel *channel);

    using EventList = std::vector<epoll_event>;

    int epollfd_;
    EventList events_;
};